package one.stand.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import one.stand.aop.AdminController;
import one.stand.aop.ApiController;
import one.stand.code.RedisCode;
import one.stand.entity.TokenEntity;
import one.stand.enums.ResultEnum;
import one.stand.exception.*;
import one.stand.feign.AdminFegin;
import one.stand.model.ResultModel;
import one.stand.request.BusinessCardRequest;
import one.stand.request.TokenRequest;
import one.stand.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Administrator
 */
@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {

    protected static final Logger log = LoggerFactory.getLogger(LoginInterceptor.class);

    @Autowired
    RedisUtil redisUtil;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        httpServletResponse.setHeader("Content-type", "application/json;charset=UTF-8");
        StringBuffer requestURL = httpServletRequest.getRequestURL();
        log.info("前置拦截器1 preHandle： 请求的uri为：" + requestURL.toString());
        try {
            String token = httpServletRequest.getHeader("token");
            //未获取到token抛出异常
            if (StringUtils.isEmpty(token)) {
                throw new LoginException("token请求头未设置");
            }
            TokenEntity tokenEntity = redisUtil.get(RedisCode.getAdminToken(token));
            if (tokenEntity == null) {
                //token在login的时候生成返回，由admin端在header里携带
                throw new LoginException("Token信息不存在或已过期，请重新登录admin-pc端");
            }
            if (o instanceof HandlerMethod) {
                HandlerMethod h = (HandlerMethod) o;
                AdminController adminController = h.getMethodAnnotation(AdminController.class);
                if (adminController != null) {

                    //验证是否需要进行登陆校验
                    if (adminController.tourist()) {
                        if (tokenEntity.getTourist() != 1) {
                            throw new TouristException();
                        }
                    }
                    if (adminController.isTourist()) {
                        TokenRequest tokenRequest = new TokenRequest();
                        BeanUtils.copyProperties(tokenEntity, tokenRequest);
                        httpServletRequest.setAttribute("tokenRequest", tokenRequest);
                    }
                }
            }
            return true;
        } catch (LoginException l) {
            ResultModel.returnJson(httpServletResponse, ResultEnum.NO_LOGIN, l.getMessage());
        } catch (TouristException t) {
            ResultModel.returnJson(httpServletResponse, ResultEnum.NO_TOURIST, t.getMessage());
        } catch (Exception e) {
            ResultModel.returnJson(httpServletResponse, ResultEnum.ERROR, e.getMessage());
        }
        return false;
    }
}
